﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class Driver : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chbIsActive;
        protected DropDownList ddlDriverCardType;
        protected DropDownList ddlPriority;
        protected DropDownList ddlSex;
        protected HtmlForm F2221;
        protected HtmlInputHidden hdnFrom;
        protected Label lblAddress;
        protected Label lblAge;
        protected Label lblCarrierID;
        protected Label lblCity;
        protected Label lblDriverCard;
        protected Label lblDriverID;
        protected Label lblDriverType;
        protected Label lblFax;
        protected Label lblIDCard;
        protected Label lblMsg;
        protected Label lblName;
        protected Label lblOwner;
        protected Label lblPriority;
        protected Label lblProvince;
        protected Label lblRemark;
        protected Label lblSex;
        protected Label lblTel;
        protected Label lblZip;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        protected TextBox txtAddress;
        protected TextBox txtAge;
        protected TextBox txtCarrierID;
        protected TextBox txtCity;
        protected TextBox txtDriverCard;
        protected TextBox txtDriverID;
        protected TextBox txtFax;
        protected TextBox txtIDCard;
        protected TextBox txtName;
        protected TextBox txtProvince;
        protected TextBox txtRemark;
        protected TextBox txtTel;
        protected TextBox txtZip;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.lblMsg.Text = "";
            this.txtDriverID.Text = "";
            this.txtDriverID.Enabled = true;
            base.DataEntityViewState = null;
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (this.txtDriverID.Text.Trim() != "")
                {
                    List<string> driverIDs = new List<string> {
                        this.txtDriverID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(driverIDs, "DRIVER") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("DriverQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("DriverList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (this.txtDriverID.Text.Trim() == "")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2510_001");
            }
            else if (this.txtCarrierID.Text.Trim() == "")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2510_002");
            }
            else if (this.txtName.Text.Trim() == "")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2510_003");
            }
            else if (this.txtDriverCard.Text.Trim() == "")
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F2510_004");
            }
            else if (base.DataEntityViewState != null)
            {
                this.UpdateDriverInfo();
            }
            else
            {
                this.InsertDriverInfo();
            }
        }

        private DriverInfo GetDriverInfo(DriverInfo driverInfo)
        {
            int intTemp = 0;
            driverInfo.DriverID = this.txtDriverID.Text.Trim();
            driverInfo.CarrierID = this.txtCarrierID.Text.Trim();
            driverInfo.Sex = this.ddlSex.SelectedValue;
            if (int.TryParse(this.txtAge.Text.Trim(), out intTemp))
            {
                driverInfo.Age = int.Parse(this.txtAge.Text.Trim());
            }
            else
            {
                driverInfo.Age = 0;
            }
            driverInfo.DriverName = this.txtName.Text.Trim();
            driverInfo.IdCard = this.txtIDCard.Text.Trim();
            driverInfo.DriverCardNo = this.txtDriverCard.Text.Trim();
            driverInfo.DriverCardType = this.ddlDriverCardType.SelectedValue;
            driverInfo.Fax = this.txtFax.Text.Trim();
            driverInfo.City = this.txtCity.Text.Trim();
            driverInfo.Province = this.txtProvince.Text.Trim();
            driverInfo.Zip = this.txtZip.Text.Trim();
            driverInfo.Telephone = this.txtTel.Text.Trim();
            driverInfo.Address = this.txtAddress.Text.Trim();
            driverInfo.Priority = this.ddlPriority.SelectedValue;
            driverInfo.Remark = this.txtRemark.Text.Trim();
            driverInfo.IsActive = this.chbIsActive.Checked ? "Y" : "N";
            return driverInfo;
        }

        private void InsertDriverInfo()
        {
            try
            {
                DriverInfo driverInfo = new DriverInfo();
                driverInfo = this.GetDriverInfo(driverInfo);
                driverInfo.CreatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(driverInfo, "DRIVER") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = driverInfo;
                this.txtDriverID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlDriverCardType, "DriverLicenseType");
            base.BindData2DDLBySysCode(this.ddlPriority, "OrderPriority");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.SetDriverInfo2Form();
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetDriverInfo2Form()
        {
            try
            {
                string driverID = base.Request.QueryString["ID"].ToString();
                DriverInfo driverInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(driverID, "DRIVER") as DriverInfo;
                base.DataEntityViewState = driverInfo;
                if (driverInfo != null)
                {
                    this.txtDriverID.Text = driverInfo.DriverID;
                    this.txtDriverID.Enabled = false;
                    this.txtCarrierID.Text = driverInfo.CarrierID;
                    this.ddlSex.SelectedValue = driverInfo.Sex;
                    this.txtAge.Text = driverInfo.Age.ToString();
                    this.txtName.Text = driverInfo.DriverName;
                    this.txtIDCard.Text = driverInfo.IdCard;
                    this.txtDriverCard.Text = driverInfo.DriverCardNo;
                    this.ddlDriverCardType.SelectedValue = driverInfo.DriverCardType;
                    this.txtFax.Text = driverInfo.Fax;
                    this.txtCity.Text = driverInfo.City;
                    this.txtProvince.Text = driverInfo.Province;
                    this.txtZip.Text = driverInfo.Zip;
                    this.txtTel.Text = driverInfo.Telephone;
                    this.txtAddress.Text = driverInfo.Address;
                    if (!string.IsNullOrEmpty(driverInfo.Priority))
                    {
                        this.ddlPriority.SelectedValue = driverInfo.Priority;
                    }
                    this.txtRemark.Text = driverInfo.Remark;
                    this.chbIsActive.Checked = driverInfo.IsActive == "Y";
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateDriverInfo()
        {
            try
            {
                DriverInfo driverInfo = base.DataEntityViewState as DriverInfo;
                driverInfo = this.GetDriverInfo(driverInfo);
                driverInfo.UpdatedBy = base.UserInfo.UserID;
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(driverInfo, "DRIVER") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

